{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.rcParams['font.sans-serif'] = ['SimHei']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 收集实验数据\n",
    "通过实验，收集数据，记录在下表中\n",
    "\n",
    "i | 1| 2| 3| 4| 5| 6| 7| 8\n",
    "- | :-: | :-: | :-: | :-: | :-: | :-: | :-: | -:\n",
    "弹簧长度 |25.05| 26.51| 29.33| 31.07| 32.8| 35.05| 36.72| 40.05\n",
    "砝码重量 |0| 10| 20| 30|40| 50| 60| 70"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 弹簧长度（cm）\n",
    "x = [25.05, 26.51, 29.33, 31.07, 32.8, 35.05, 36.72, 40.05]\n",
    "#砝码重量（g）\n",
    "y = [0, 10, 20, 30, 40, 50, 60, 70]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 在坐标系中画出散点图，猜测模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAtUAAAIzCAYAAADPruX0AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuNCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8QVMy6AAAACXBIWXMAAAsTAAALEwEAmpwYAAAmh0lEQVR4nO3dfZBd933f988XWBBmV3EsUSBDUhJI2jJTj6cVkA3rjAa1KYsd+mFEMDNJpRox/UCyCZqMlda16Xiapu20w8YZR0knaEpSjuhAE0Y2TUpVIzkcmo7h1hKzACQ/hGJpk4IogBFg2K7NNQ3uw69/4FKC8bC42B+w517g9ZrZuXvPPXvxxZkFznvPnntutdYCAACs3YahBwAAgGknqgEAoJOoBgCATqIaAAA6iWo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wh/mOttU9W1a/l5K+QfzjJl5L8lSGHnEDn2m6/nWRzkqdGv33/TGvtrw845yQ667YbeKZpca7vu6tiP3E+59p2fxD7ibV4MFO4j5iqS+oBAMAkmqrTPwAAYBKJagAA6CSqAQCgk6gGAIBOohoAADqJagAA6CSqAS6Cqrqpqtp5Pr5YVT8wxnofGT3nR8ZY9wdG635xjHVvGnfOgbbh+eZyDVhgYolqgIvncGutzvaRk2+T/YaPrrLeD572nPetsu6jp637zlXWPbSGOYewaZXZACaWqAYAgE6iGoBVVdWfq6p/XFUvVtWJqjpaVU9V1R2nrPPLo1NQbqqqJ6rqD6rq90ensLypqjZU1d+pqpeq6k+q6kBVvXvIvxfAxTQz9AAATK6quinJ/53kuiQ/m2Q+yWySb0vy3iRPnbL6bJJfSvIrSR5I8heT/FCSr0tyPMl/kuR/S7IpyY8m+T+ramtr7Y/W4+8CcCmJagBWsyfJDUnubK394qkPVNXpv+18a5K/31r7qdH9f1pVb07yV5McSPKXWmuLo699LsnHk/wXSf6PSzg/wLpw+gcAZ1VVb0lyZ5JPnx7USdJaWzlt0XJOHok+1b4kleSfvhHUpyxPkndepHEBBiWqATiXb8rJID445vqvtNb+5LRlvz+6fenUha21N5Zfs/bxACaHqAbgXN64jN2414deXsNjLpUHXBZENQDn8kJOBvW2oQcBmHSiGoCzaq39XpJPJfmuqnrv6Y9XlaPMACOu/gHAav5mkv8nyaeq6tEk+5NcnZOXx/tikh8fbjSAySGqATin1tpLVTWX5L9L8t1Jvj8nX3z4+SQPDTkbwCQR1QCsqrV2OMlfP88633GO5R9J8pFzPOb0EeCy4ZxqAADoJKoBLp4bq6qd7SN/+jrN37fKev/stOd8eJV17zlt3RdWWXfrGuYcwuIqswFMrGrN/1MAANDDkWoAAOgkqgEAoJOoBgCATqIaAAA6iWoAAOgkqgEAoJOoBgCATv8/fvun03MwEfMAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 864x648 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.figure(figsize=(12,9))\n",
    "plt.xlim(24,41)\n",
    "plt.scatter(x,y,100)\n",
    "plt.xlabel('弹簧长度（cm）',fontsize=18)\n",
    "plt.ylabel('砝码重量（g）',fontsize=18)\n",
    "plt.title('胡克定律实验',fontsize=20)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  构建一个最小二乘法函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "def least_square(x, y):\n",
    "    if len(x) == 2:   \n",
    "    # 此时x为自然序列\n",
    "        sx = 0.5 * (x[1] - x[0] + 1) * (x[1] + x[0])\n",
    "        ex = sx / (x[1] - x[0] + 1)\n",
    "        sx2 = ((x[1] * (x[1] + 1) * (2 * x[1] + 1))\n",
    "               - (x[0] * (x[0] - 1) * (2 * x[0] - 1))) / 6\n",
    "        x = np.array(range(x[0], x[1] + 1))\n",
    "    else:\n",
    "        sx = sum(x)\n",
    "        ex = sx / len(x)\n",
    "        sx2 = sum(x ** 2)\n",
    "\n",
    "    sxy = sum(x * y)\n",
    "    ey = np.mean(y)\n",
    "\n",
    "    a = (sxy - ey * sx) / (sx2 - ex * sx)\n",
    "    b = (ey * sx2 - sxy * ex) / (sx2 - ex * sx)\n",
    "    return a, b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算出K，B的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型的k值是：4.768895049688474\n",
      "模型的b值是：-117.95038648113349\n"
     ]
    }
   ],
   "source": [
    "train_x = np.array(x)\n",
    "train_y = np.array(y)\n",
    "k,b = least_square(train_x,train_y)\n",
    "print(\"模型的k值是：{}\".format(k))\n",
    "print(\"模型的b值是：{}\".format(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 当弹簧为50cm时，测量重物的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "重物的质量为：120.49436600329021\n"
     ]
    }
   ],
   "source": [
    "# 已知弹簧伸长的长度：\n",
    "x = 50\n",
    "# 求出 重物的重量\n",
    "y = k * x + b\n",
    "print(\"重物的质量为：{}\".format(y))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
